import HFRequest from "./index";
import router from "@/router/index";

export const hfoRequest = new HFRequest(
  {
    baseURL: appConfigObj.AXIOS_BASEURL,
    interceptors: {
      requestInterceptor: (config) => {
        return config;
      },
      requestInterceptorCatch: (err) => {
        return err;
      },
      responseInterceptor: (res) => {
        return { data: res.data, status: res.status };
      },
      responseInterceptorCatch: (error) => {
        let res: Error | undefined;
        showNotify({ type: "warning", message: error.response.data.message });
        if ([401, 403].includes(error.response.status)) {
          router.push("/login");
          throw res;
        }
        if (res instanceof Error) throw res;
        return { data: error.response.data, status: false };
      }
    }
  },
  (config) => {
    const useStore = useAllStore();
    if (useStore.token && config.url) {
      config.headers = {};
      config.headers.Authorization = useStore.token;
    }
  }
);
